package com.example.testi18n.exception.constant;

import com.example.testi18n.constant.LanguageConstant;
import com.example.testi18n.exception.IResponseEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Locale;

/**
 * @author xiaobendan
 * @version v1.0.0
 * @className BaseThrowException
 * @package com.example.testi18n.exception.constant
 * @description 系统异常枚举类
 * @date 2022-09-26 15:04
 */
@Getter
@AllArgsConstructor
public enum BaseThrowException implements IResponseEnum {

    /**
     * 系统错误
     */
    SERVER_ERROR(500, "系统错误","System Error"),

    SQL_ERROR(500, "sql语句错误","SQL Error"),
    /*
     * 服务端任意异常
     * */
    SERVICE_EXCEPTION(500, "{0}","{0}"),
    /**
     * 任意错误语句
     */
    MESSAGE(403, "{0}","{0}"),
    /**
     * 参数为空
     */
    ARG_IS_EMPTY(405, "{0}不能为空",""),

    /**
     * feign错误
     */
    FEIGN_ERROR(501, "feign错误","Feign error"),
    /**
     * 用户未登录
     */
    USER_NOT_LOGIN(401, "用户身份过期","User identity expires"),
    /**
     * 参数错误
     */
    VALID_ERROR(402, "参数错误","Parameter Error"),

    /**
     * 页面不存在
     */
    PAGE_NOT_FOUND(404, "页面不存在","Page does not exist"),
    /**
     * 请求方式错误
     */
    REQUEST_NOT_VALID(406, "请求方式错误","Request method error"),
    /**
     * 权限不足
     */
    PERMISSION_LIMIT(900, "用户权限不足","Insufficient user privileges"),

    ;
    /**
     * 返回码
     */
    private final int code;

    /**
     * 返回消息
     */
    private final String message;

    /**
     * 国际化消息
     */
    private final String enMessage;

    /**
     * 错误消息
     */
    @Override
    public String getI18nMessage(){
        Locale locale = LocaleContextHolder.getLocale();
        if (LanguageConstant.CHINESE.equals(locale.toString())){
            return getMessage();
        }
        return getEnMessage();
    }
}
